<%

dim lConnection, lConnectionString, RS

lConnectionString = request.form("cs")
if len(lConnectionString) > 0 then
	session("ConnectionString") = lConnectionString
end if

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>SQL query tool</title>
	<style>
		body {
			background-color: white;
			font-family: Arial;
			font-size: 11px;
		}
		
		td {
			font-family: Arial;
			font-size: 11px;
			border-top: 1px dotted black;
			border-left: 1px dotted black;
		}
		
		.last {
			font-family: Arial;
			font-size: 11px;
			border-top: 1px dotted black;
			border-left: 1px dotted black;
			border-right: 1px dotted black;
		}
		
		.lastbottom {
			font-family: Arial;
			font-size: 11px;
			border-top: 1px dotted black;
			border-left: 1px dotted black;
			border-right: 1px dotted black;
			border-bottom: 1px dotted black;
		}
		
		.bottom {
			font-family: Arial;
			font-size: 11px;
			border-top: 1px dotted black;
			border-left: 1px dotted black;
			border-bottom: 1px dotted black;
		}
	</style>
</head>

<body>

<form name="qf" method="post" action="?command=query">
	Connectionstring:<br>
	<input type="Text" name="cs" size="75" value="<%=session("ConnectionString")%>"><br><br>
	<textarea name="q" rows=10 cols=60></textarea><br><br>
	<input type="submit" value="RUN">	
</form>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
<% if session("ConnectionString") = "" then %>
document.qf.setfocus();
<% else %>
document.qf.q.focus();
<% end if %>
//-->
</SCRIPT>

<table cellpadding="3" cellspacing="0">
<%

if request.querystring("command") = "query" and len(request.form("q")) > 0 then
	openDB
	
	set RS = lConnection.execute(request.form("q"))
	%><tr><%
	for t = 0 to RS.fields.count - 1
		%><td<% if t=RS.fields.count - 1 then response.write " class=last"%>><strong><%=RS(t).name%></strong></td><%
	next
	%></tr><%
	
	while not RS.eof
		%><tr><%
		for t = 0 to RS.fields.count - 1
			%><td<% if t=RS.fields.count - 1 then response.write " class=last"%>><%=asString(RS(t).value)%></td><%
		next
		%></tr><%
		RS.movenext
	wend
	
	%><tr><%
	for t = 0 to RS.fields.count - 1
		%><td<% if t=RS.fields.count - 1 then response.write " class=lastbottom" else response.write " class=bottom"%>><strong><%=RS(t).name%></strong></td><%
	next
	%></tr><%
	
	closeDB
end if

function asString(wvWhat)
	asString = wvWhat & ""
	if asString = "" then asString = "&nbsp;"
end function

sub openDB
	' opens the database connection
	Set lConnection = Server.CreateObject("ADODB.Connection")
	lConnection.Open lConnectionString
end sub
	
sub closeDB
	' closes the database connection
	lConnection.close
	Set lConnection = nothing
end sub

%></table>

</body>
</html>